﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TWS.Cart.Data;

public partial class Application_Controls_Utils_Login : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

	/// <summary>
	/// Event that fires when Login is successful
	/// </summary>
	public event EventHandler LoggedIn;


	public void ButtonLogin_Click(object sender, EventArgs e)
	{
		//UserCollection ssUser = new UserCollection()
		//    .Where(User.Columns.Email,TextBoxLoginUserNameEmail.Text)
		//    .Where(User.Columns.Password,TextBoxLoginPassword.Text)
		//    .Load();
		ViewUserWithZipoCodeCollection ssUser = new ViewUserWithZipoCodeCollection()
			.Where(ViewUserWithZipoCode.Columns.Email, TextBoxLoginUserNameEmail.Text)
			.Where(ViewUserWithZipoCode.Columns.Password, TextBoxLoginPassword.Text)
			.Load();

		if (ssUser.Count > 0)
		{
			TWSUser twsUser = new TWSUser();
			twsUser.Id = ssUser[0].Id;
			twsUser.FullName = ssUser[0].FullName;
			twsUser.Email = ssUser[0].Email;
			twsUser.ZipCode = ssUser[0].ZipCode;

			Session["twsUser"] = twsUser;

			if (this.LoggedIn != null)
				this.LoggedIn(this, new EventArgs());
		}
		else
		{
			LabelLoginMessage.Text = Resources.Resource.UserNotFound;
		}
	}
}
